# -*- coding: utf-8 -*-
# @Time    : 2024/4/20 9:05 下午
# @Author  : Hoey

# pymysql 是一个开源的工具，已经写好了链接工具，只需要提供ip、端口、用户名
from pymysql import Connection

# 链接
from day04.加载资源 import get_mysql_config

conn = Connection(
    host=get_mysql_config('host'),
    port=get_mysql_config('port'),
    user=get_mysql_config('user'),
    password=get_mysql_config('password'),
    database=get_mysql_config('databse')
)


# 查询数据库表test的值
def select():
    cur = conn.cursor()
    cur.execute('select * from test1;')
    result = cur.fetchall()
    print(f"结果: {result}, 类型: {type(result)}")
    cur.close()


# 创建表
def create():
    cur = conn.cursor()
    sql = """
    create table test2(
        id int,
        name varchar(255)
    );
    """
    cur.execute(sql)
    cur.close()


# 插入数据
def insert():
    cur = conn.cursor()
    sql = """
        insert into test2(id, name) values(1, 'maodandan')
    """
    cur.execute(sql)
    conn.commit()
    cur.close()


# 更新maodandan -> maodandan1
def update():
    cur = conn.cursor()
    sql = """
    update test2 set name = 'maodandan2' where name = 'maodandan'
    """
    cur.execute(sql)
    conn.commit()
    cur.close()


if __name__ == '__main__':
    create()
